const Koa = require('koa')
const app = new Koa()
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const static = require('koa-static')

const util = require('./util')
const ajax = require('./util/ajax')
const logger = require('./middleware/logger')
const jwt = require('./middleware/jwt')
const auth = require('./middleware/auth')
const statusCode = require('./config/statusCode')

onerror(app)
app.context.util = util
app.context.ajax = ajax
app.context.stop = function (codeName) {
  this.body = statusCode[codeName] || statusCode.SUCCESS
}
app.use(bodyparser({
  enableTypes: ['json', 'form', 'text']
}))
app.use(json())
app.use(static(__dirname + '/public'))
// 日志
app.use(logger())
// token验证
jwt(app)
// 权限验证
auth(app)

module.exports = app
